﻿using AutoMapper;
using BasicInformation.ApiWrite.Application.Command.Client;
using BasicInformation.Domain;
using BasicInformation.ErrorCode;
using BasicInformation.Infrastructure.Implement;
using MediatR;

namespace BasicInformation.ApiWrite.Application.Handler.Client
{
    /// <summary>
    /// 客户管理处理类
    /// </summary>
    public class AddClientHandler : IRequestHandler<AddClientCommand, ApiResult<int>>
    {
        private readonly DatabaseContext db;
        private readonly ILogger<AddClientHandler> logger;
        private readonly IMapper mapper;

        public AddClientHandler(DatabaseContext db,ILogger<AddClientHandler> logger,IMapper mapper)
        {
            this.db = db;
            this.logger = logger;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(AddClientCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();
                var client = mapper.Map<ClientModel>(request);
               var model=db.SqlSugar.Queryable<ClientModel>().Where(x=>x.ClientCode==request.ClientCode).Count();
                if (model >0)
                {
                    result.Msg = "该客户管理已存在，请重新输入";
                    result.Code = ApiEnum.Fail;
                    logger.LogError(result.Msg);
                    return await Task.FromResult(result);
                }
                //if (!string.IsNullOrEmpty(request.ClientName))
                //{
                //    result.Msg = "客户名称不能为空，请重新输入";
                //    result.Code = ApiEnum.Fail;
                //    logger.LogError(result.Msg);
                //    return await Task.FromResult(result);
                //}
                //if (!string.IsNullOrEmpty(request.ClientTel))
                //{
                //    result.Msg = "客户电话不能为空，请重新输入";
                //    result.Code = ApiEnum.Fail;
                //    logger.LogError(result.Msg);
                //    return await Task.FromResult(result);
                //}
                //if (!string.IsNullOrEmpty(request.ClientFax))
                //{
                //    result.Msg = "客户传真不能为空，请重新输入";
                //    result.Code = ApiEnum.Fail;
                //    logger.LogError(result.Msg);
                //    return await Task.FromResult(result);
                //}
                //if (!string.IsNullOrEmpty(request.ClientEmail))
                //{
                //    result.Msg = "客户电子邮件不能为空，请重新输入";
                //    result.Code = ApiEnum.Fail;
                //    logger.LogError(result.Msg);
                //    return await Task.FromResult(result);
                //}
                //if (!string.IsNullOrEmpty(request.ClientContact))
                //{
                //    result.Msg = "客户联系人不能为空，请重新输入";
                //    result.Code = ApiEnum.Fail;
                //    logger.LogError(result.Msg);
                //    return await Task.FromResult(result);
                //}
                //if (!string.IsNullOrEmpty(request.ClientAddr))
                //{
                //    result.Msg = "客户地址不能为空，请重新输入";
                //    result.Code = ApiEnum.Fail;
                //    logger.LogError(result.Msg);
                //    return await Task.FromResult(result);
                //}
                int r = await db.SqlSugar.Insertable(client).ExecuteCommandAsync();
                if (r > 0)
                {
                    result.Msg = "客户管理添加成功";
                    result.Data = r;
                    result.Code = ApiEnum.OK;
                    logger.LogInformation(result.Msg);
                }
                else
                {
                    result.Msg = "客户管理添加失败";
                    result.Data = r;
                    result.Code = ApiEnum.Fail;
                    logger.LogError(result.Msg);
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
           
        }
    }
}
